package jnpf.engine.custom.cmd.tasks;

import jnpf.engine.util.SpringContextHolder;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;

import java.util.List;

/**
 * @since 2024/3/8
 */
public class FindMemoryTaskCmd  implements Command<TaskEntity> {
    public String processInstanceId;

    public FindMemoryTaskCmd(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    @Override
    public TaskEntity execute(CommandContext commandContext) {
        SpringProcessEngineConfiguration springProcessEngineConfiguration = SpringContextHolder.getBean(SpringProcessEngineConfiguration.class);
        TaskEntityManager taskEntityManager = springProcessEngineConfiguration.getTaskServiceConfiguration().getTaskEntityManager();
        List<TaskEntity> tasksByProcessInstanceId = taskEntityManager.findTasksByProcessInstanceId(processInstanceId);
        return tasksByProcessInstanceId.get(0);
    }
}
